import Vue from 'vue';
import Router from 'vue-router';
import store from '@/store/index.js'

Vue.use(Router);

const routes = [{
    name: '/',
    redirect: "/index"
}, {
    name: 'index',
    component: () => import('../view/index'),
    meta: {
        title: '预约挂号'
    }
}, {
    name: 'recipel',
    component: () => import('../view/recipel'),
    meta: {
        title: '诊断处方',
        showBack: true
    }
}, {
    name: 'doctor-enter',
    component: () => import('../view/doctor/enter.vue'),
    meta: {
        title: '医生主页',
        showBack: true
    }
}, {
    name: 'license',
    component: () => import('../view/license'),
    meta: {
        title: '电子执照',
        showBack: true
    }
}, {
    name: 'order-check',
    component: () => import('../view/order/check.vue'),
    meta: {
        title: '医生主页',
        showBack: true
    }
}, {
    name: 'order-detail',
    component: () => import('../view/order/detail.vue'),
    meta: {
        title: '订单详情',
        showBack: true
    }
}, {
    name: 'doctor-main',
    component: () => import('../view/doctor/main.vue'),
    meta: {
        title: '',
        showBack: true
    }
}, {
    name: 'doctor-list',
    component: () => import('../view/doctor/list.vue'),
    meta: {
        title: '',
        showBack: true
    }
}, {
    name: 'video',
    component: () => import('../view/video'),
    meta: {
        title: '视频问诊',
        showBack: true
    }
}, {
    name: 'pic-arti',
    component: () => import('../view/picArti'),
    meta: {
        title: '图文问诊',
        showBack: true
    }
}, {
    name: 'video-insert',
    component: () => import('../view/video/insert'),
    meta: {
        title: '视频问诊',
        showBack: true
    }
}, {
    path: '*',
    name: 'error_404',
    meta: {
        title: "404-页面未找到"
    },
    component: () =>
        import('@/view/common/notFound')
}];

// add route path
routes.forEach(route => {
    route.path = route.path || '/' + (route.name || '');
});

const router = new Router({ routes });

router.beforeEach((to, from, next) => {
    const title = to.meta && to.meta.title;
    if (title) {
        document.title = title;
    }
    next();
});

router.afterEach((to, from, next) => {
    store.dispatch('ToggleTitle', {
        title: to.meta.title
    });
    window.scrollTo(0, 0)
})

export {
    router
};